class FactoryParserHook {
  /**
   * @param {import('webpack').Compiler} compiler
   */
  apply(compiler) {
    const name = '个人测试 FactoryParserHook';
    compiler.hooks.compilation.tap(name, (compilation, { normalModuleFactory, contextModuleFactory }) => {
      normalModuleFactory.hooks.beforeResolve.tapAsync(name, (resolveData, callback) => {
        console.log('+++++ beforeResolve');
        callback();
      });
      normalModuleFactory.hooks.factorize.tapAsync(name, (resolveData, callback) => {
        console.log('+++++ factorize');

        callback();
      });
      normalModuleFactory.hooks.resolve.tapAsync(name, (resolveData, callback) => {
        console.log('+++++ resolve');
        callback();
      });
      /**
       * resolveForScheme 基本没有用到，目前知道的key只有 ‘data’ 和 ‘file’
       */
      normalModuleFactory.hooks.resolveForScheme.for('data').tapAsync(name, (resourceData, resolveData, callback) => {
        console.log('data +++++ resolveForScheme');
        callback();
      });
      normalModuleFactory.hooks.resolveForScheme.for('file').tapAsync(name, (resourceData, resolveData, callback) => {
        console.log('file +++++ resolveForScheme');
        callback();
      });
      normalModuleFactory.hooks.afterResolve.tapAsync(name, (resolveData, callback) => {
        console.log('+++++ afterResolve');
        callback();
      });

      normalModuleFactory.hooks.createModule.tapAsync(name, (createData, resolveData, callback) => {
        console.log('+++++ createModule');
        callback();
      });
      normalModuleFactory.hooks.module.tap(name, (module, createData, resolveData) => {
        console.log('+++++ module', module, createData, resolveData);
      });

      /**
      * 这4个hook的id只有8种：
        "javascript/auto",
        "javascript/dynamic",
        "javascript/esm",
        "json",
        "asset",
        "asset/inline",
        "asset/resource",
        "asset/source",
      */

      normalModuleFactory.hooks.createParser.for('javascript/auto').tap(name, (parserOptions) => {
        console.log('+++++ createParser');
      });
      normalModuleFactory.hooks.parser.for('javascript/auto').tap(name, (parser, parserOptions) => {
        console.log('+++++ parser');
      });
      normalModuleFactory.hooks.createGenerator.for('javascript/auto').tap(name, (generatorOptions) => {
        console.log('+++++ createGenerator');
      });
      normalModuleFactory.hooks.generator.for('javascript/auto').tap(name, (generator, generatorOptions) => {
        console.log('+++++ generator');
      });

      contextModuleFactory.hooks.beforeResolve.tapAsync(name, (resolveData, callback) => {
        console.log('+++++ beforeResolve');
        callback();
      });
    });
  }
}

module.exports = FactoryParserHook;
